www.gusucode.com > VC编写的串口调试软件 > VC编写的串口调试软件,内含Modbus协议类 支持对Modbus通讯调试/Modbus串口调试软件1.0/EditEx.cpp

    // EditEx.cpp : implementation file
//

#include "stdafx.h"
#include "modbusrtu.h"
#include "EditEx.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CEditEx

CEditEx::CEditEx()
{
}

CEditEx::~CEditEx()
{
}


BEGIN_MESSAGE_MAP(CEditEx, CEdit)
	//{{AFX_MSG_MAP(CEditEx)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	ON_COMMAND_RANGE(ID_EDIT_FIRST, ID_EDIT_LAST, OnEditCommand)
	ON_UPDATE_COMMAND_UI_RANGE(ID_EDIT_FIRST, ID_EDIT_LAST, OnUpdateEditCommand)
	ON_COMMAND(ID_EDIT_TO_INT, OnToInt)
	ON_UPDATE_COMMAND_UI(ID_EDIT_TO_INT, OnUpdateEditToInt)
	//}}AFX_MSG_MAP
	ON_COMMAND(ID_CHECK_INFO, &CEditEx::OnCheckInfo)
END_MESSAGE_MAP()

//	ON_COMMAND(ID_EDIT_TO_INT, OnToInt)


/////////////////////////////////////////////////////////////////////////////
// CEditEx message handlers

BOOL CEditEx::PreTranslateMessage(MSG* pMsg) 
{
	// TODO: Add your specialized code here and/or call the base class
	
	return CEdit::PreTranslateMessage(pMsg);
}

void CEditEx::PreSubclassWindow() 
{
	// TODO: Add your specialized code here and/or call the base class
	m_editMenuHandler.Install(this, IDR_EDITMENU);
	
//	CEdit::PreSubclassWindow();
}

//////////////////
// Update edit commands
//
void CEditEx::OnUpdateEditCommand(CCmdUI* pCmdUI)
{
	if (!m_editMenuHandler.OnUpdateEditCommand(pCmdUI)) {
		// update other edit command if you like
	}
}

//////////////////
// Process edit commands
//
void CEditEx::OnEditCommand(UINT nID)
{
	if (!m_editMenuHandler.OnEditCommand(nID)) {
		// handle other edit command if you like
	}
}


#include "ComData.h"
#include "Verity.h"

void CEditEx::OnToInt()
{
	int nStartChar=0;
	int nEndChar=0;
	
	GetSel (nStartChar,nEndChar);
	TRACE("CEditEx::OnToInt() nStartChar=%d,nEndChar=%d\n",nStartChar,nEndChar);
	
	CString strTxt;
	CString strSelText;
	this->GetWindowText(strTxt);
	strSelText=strTxt.Mid (nStartChar,nEndChar - nStartChar);
	strSelText.TrimLeft ();
	strSelText.TrimRight ();
	//
	CVerity Verity;
	CComData ComData;
	int nCount = Verity.TransStrHex (strSelText,ComData.m_ucRcvTxt,100);
	ComData.m_unRcvBegin = 0;
	ComData.m_unRcvCount = nCount;
	switch(nCount)
	{
	case 1:
		{
		ComData.m_unDataType=CComData::V_UN8;
		break;
		}
	case 2:
		{
		ComData.m_unDataType=CComData::V_UN16;
		break;
		}
	case 4:
		{
		ComData.m_unDataType=CComData::V_UN32;
		break;
		}
	default:
		{
		AfxMessageBox("选择的十六进制数据必须是1/2/4个");
		return;
		}
	}
//	ComData.m_unDataType=CComData::V_UN32;
	CString strValue=ComData.GetValue (true);
	TRACE("CEditEx::OnToInt() strValue=%s\n",strValue);
	
	CString strTip;
	strTip.Format ("十六进制数据 %s 转换为十进制等于 %s",strSelText,strValue);
	AfxMessageBox(strTip);

}

void CEditEx::OnUpdateEditToInt(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	int nStartChar=-1;
	int nEndChar=0;
	
	GetSel (nStartChar,nEndChar);
	if(nStartChar!=nEndChar)
	{
		CString strTxt;
		CString strSelText;
		this->GetWindowText(strTxt);
		strSelText=strTxt.Mid (nStartChar,nEndChar-nStartChar);
		strSelText.TrimLeft ();
		strSelText.TrimRight ();
		if(strSelText.IsEmpty ())
			pCmdUI->Enable (FALSE);
		else
			pCmdUI->Enable (TRUE);
	}
	else
		pCmdUI->Enable (FALSE);

}


void CEditEx::OnCheckInfo()
{
	// 
	int nStartChar=0;
	int nEndChar=0;
	
	GetSel (nStartChar,nEndChar);
	TRACE("CEditEx::OnToInt() nStartChar=%d,nEndChar=%d\n",nStartChar,nEndChar);
	
	CString strTxt;
	CString strSelText;
	this->GetWindowText(strTxt);
	strSelText=strTxt.Mid (nStartChar,nEndChar-nStartChar);
	strSelText.TrimLeft ();
	strSelText.TrimRight ();

	// 1. 计算字符数量

	//
	CVerity Verity;
	CComData ComData;
	int nCount = Verity.TransStrHex (strSelText,ComData.m_ucRcvTxt,100);
	ComData.m_unRcvBegin =0;
	ComData.m_unRcvCount = nCount;
	
	// 2. CRC

	UINT unCRC16 =  Verity.CRC16 (ComData.m_ucRcvTxt,nCount);

	// 3. LRC
	UINT unLRC =  Verity.LRC (strSelText);

	// 4. SumHex
	UINT unSumHex = Verity.SumHex (ComData.m_ucRcvTxt,nCount);

	// 5. SumAscNum
	UINT unSumAscNum = Verity.SumAscNum (ComData.m_ucRcvTxt,nCount);


	// 显示

	char cEnty[3];
	cEnty[0] = 13;
	cEnty[1] = 10;
	cEnty[2] = '\0';


	CString strVerity;
	CString strCount;
	CString strCRC;
	CString strLRC;
	CString strSumHex;
	CString strSumAscNum;

	strCount.Format (_T("字符总数: 0x%04x; %5d"),nCount,nCount);

	strCRC.Format (_T("CRC校验: 0x%04x"),unCRC16);

	strLRC.Format (_T("LRC校验: 0x%04x"),unLRC);


	strSumHex.Format (_T("Hex和校验: 0x%06x ; %d"),unSumHex,(long)unSumHex);

	strSumAscNum.Format (_T("数字和校验: 0x%06x ; %d"),unSumAscNum,(long)unSumAscNum);

	strVerity = strCount ;
	strVerity = strVerity + cEnty  + cEnty + strCRC;
	strVerity = strVerity + cEnty  + cEnty + strLRC;
	strVerity = strVerity + cEnty  + cEnty + strSumHex;
	strVerity = strVerity + cEnty  + cEnty + strSumAscNum;



	AfxMessageBox(strVerity);

}